package cn.shyouchi.wuguanjia.android.ui.attendance.outside;

import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.amap.api.maps.AMap;
import com.amap.api.maps.CameraUpdateFactory;
import com.amap.api.maps.LocationSource;
import com.amap.api.maps.MapView;
import com.amap.api.maps.UiSettings;
import com.amap.api.maps.model.BitmapDescriptorFactory;
import com.amap.api.maps.model.Circle;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.Marker;
import com.amap.api.maps.model.MarkerOptions;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.shyouchi.wuguanjia.android.R;
import cn.shyouchi.wuguanjia.android.base.BaseActivity;
import cn.shyouchi.wuguanjia.android.utils.LogUtil;
import cn.shyouchi.wuguanjia.android.widget.TopBar;

public class OutsideActivity extends BaseActivity implements
        LocationSource,
        AMapLocationListener {

    @BindView(R.id.topBar)
    TopBar mTopBar;
    @BindView(R.id.tv_location)
    TextView mTvLocation;
    @BindView(R.id.tv_time)
    TextView mTvTime;
    @BindView(R.id.map)
    MapView mMapView;
    @BindView(R.id.btn_sign_out)
    LinearLayout mBtnOut;
    @BindView(R.id.btn_sign_in)
    LinearLayout mBtnIn;

    AMap mMap;

    UiSettings mUiSettings;
    //声明AMapLocationClient类对象
    public AMapLocationClient mLocationClient = null;
    AMapLocationClientOption mLocationClientOption;

    Marker mLocationMarker;
    Circle mLocationCircle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mMapView.onCreate(savedInstanceState);
    }

    @Override
    public int initContentView() {
        return R.layout.activity_outside;
    }

    @Override
    public void initInjector() {

    }

    @Override
    public void initUiAndListener() {
        ButterKnife.bind(this);
        mTopBar.setTitle(getTitle().toString());
        mTopBar.setLeftIcon(R.mipmap.ic_back);
        setSupportActionBar(mTopBar.getToolbar());


        mMap = mMapView.getMap();



//        initMap();
        initLocation();

    }

    @Override
    protected boolean isApplyStatusBarTranslucency() {
        return false;
    }

    @Override
    protected boolean isApplyStatusBarColor() {
        return true;
    }

//    @OnClick(R.id.btn_take_photo)
//    public void onClick() {
//
//
//    }
//
//    @OnClick(R.id.tv_sel_rank)
//    public void onSelRankClick(View v) {
//
//
//    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //在activity执行onDestroy时执行mMapView.onDestroy()，实现地图生命周期管理
        mMapView.onDestroy();
    }

    @Override
    protected void onResume() {
        super.onResume();
        //在activity执行onResume时执行mMapView.onResume ()，实现地图生命周期管理
        mMapView.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        //在activity执行onPause时执行mMapView.onPause ()，实现地图生命周期管理
        mMapView.onPause();
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        //在activity执行onSaveInstanceState时执行mMapView.onSaveInstanceState (outState)，实现地图生命周期管理
        mMapView.onSaveInstanceState(outState);
    }


    private void initLocation() {
        //初始化定位
        mLocationClient = new AMapLocationClient(getApplicationContext());
        mLocationClientOption = new AMapLocationClientOption();


        //设置定位回调监听
        mLocationClient.setLocationListener(this);

        //设置定位一次、该方法默认为false。
        mLocationClientOption.setOnceLocation(true);

        //设置setOnceLocationLatest(boolean b)接口为true，启动定位时SDK会返回最近3s内精度最高的一次定位结果。如果设置其为true，setOnceLocation(boolean b)接口也会被设置为true，反之不会，默认为false。
        mLocationClientOption.setOnceLocationLatest(true);

        mLocationClient.setLocationOption(mLocationClientOption);
        mLocationClient.startLocation();
    }

    private void initMap() {
        mUiSettings = mMap.getUiSettings();
        mMap.setMapType(AMap.MAP_TYPE_NORMAL);
        mMap.setLocationSource(this);// 设置定位监听
        mUiSettings.setMyLocationButtonEnabled(true); // 显示默认的定位按钮
        mMap.setMyLocationEnabled(true);// 可触发定位并显示定位层
        mUiSettings.setScaleControlsEnabled(true);//显示比例尺控件
        mUiSettings.setCompassEnabled(true);
    }

    @Override
    public void onLocationChanged(AMapLocation location) {
        LogUtil.json(location);
        if (location != null && location.getErrorCode() == 0) {
            LatLng lng = new LatLng(location.getLatitude(), location.getLongitude());
            MarkerOptions markerOptions = new MarkerOptions()
                    .position(lng)
                    .icon(BitmapDescriptorFactory.fromResource(R.mipmap.location_marker));
            if (mLocationMarker == null) {
                mLocationMarker = mMap.addMarker(markerOptions);
//                mLocationCircle = mMap.addCircle(new CircleOptions().
//                        center(lng).
//                        radius(1000).
//                        fillColor(Color.argb(50, 1, 1, 1)).
//                        strokeColor(Color.argb(50, 1, 1, 1)).
//                        strokeWidth(15));

            } else {
                mLocationMarker.setPosition(lng);
            }

            mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(lng, 19));
        }
    }

    @Override
    public void activate(LocationSource.OnLocationChangedListener onLocationChangedListener) {
        mLocationClient.startLocation();
    }

    @Override
    public void deactivate() {

    }



}
